package com.example.moblie2030606150cjk0618;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rvMainUsers;
    private Button btnMainInsert;
    private List<Users> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list = new MyDataAdapter().QueryAll(this);
        rvMainUsers.setAdapter(new UsersRViewAdapter(list));
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_insert, null);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("添加数据")
                        .setView(view)
                        .setPositiveButton("添加", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = ((EditText) view.findViewById(R.id.edtTxtDialogName))
                                        .getText().toString().trim();
                                int age = Integer.valueOf(((EditText) view.findViewById(R.id.edtTxtDialogAge))
                                        .getText().toString().trim());
                                float score =Float.valueOf(((EditText) view.findViewById(R.id.edtTxtDialogScore))
                                        .getText().toString().trim());
                                new MyDataAdapter().insert(MainActivity.this, new Users(name, age, score));
                                list.clear();
                                list.addAll(new MyDataAdapter().QueryAll(MainActivity.this));
                                rvMainUsers.getAdapter().notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    private void initView() {
        rvMainUsers = (RecyclerView) findViewById(R.id.rvMainUsers);
        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        rvMainUsers.setLayoutManager(new LinearLayoutManager(this));
    }
}